﻿#region Copyright
/* © 2008 George Tsiokos, licensed under the Microsoft Reciprocal License (Ms-RL) */
#endregion
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Threading;

namespace Comet.Web {
    public abstract class CometHttpHandler : IHttpAsyncHandler {

		#region Properties (1) 

        public abstract bool IsReusable {
            get;
        }

		#endregion Properties 

		#region Methods (6) 


		// Public Methods (6) 

        public IAsyncResult BeginProcessRequest (HttpContext context, AsyncCallback cb, object extraData) {
            if (RequestReceived (context))
                return new CometAsyncResult (context, extraData, GetWaitHandle ());
            else
                return null;
        }

        public void EndProcessRequest (IAsyncResult result) {
            CometAsyncResult cometAsyncResult = result as CometAsyncResult;
            if (null == cometAsyncResult)
                throw new InvalidOperationException ();
            HandleRequest (cometAsyncResult.HttpContext);
            cometAsyncResult.Completed ();
        }

        public abstract WaitHandle GetWaitHandle ();

        public abstract void HandleRequest (HttpContext context);

        public void ProcessRequest (HttpContext context) {
            throw new NotSupportedException ("only async invocation is supported");
        }

        public virtual bool RequestReceived (HttpContext context) {
            return true;
        }


		#endregion Methods 

    }
}